package com.zhupanlin.dao;

import com.zhupanlin.io.Resources;
import com.zhupanlin.pojo.User;
import com.zhupanlin.sqlSession.SqlSession;
import com.zhupanlin.sqlSession.SqlSessionFactory;
import com.zhupanlin.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * 描述信息：
 *
 * @author zhupanlin
 * @version 1.0
 * @date 2025/2/10 14:20
 */
public class UserDaoImpl implements IUserDao{
    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        List<User> users = sqlSession.selectList("user.selectList");
        System.out.println("users = " + users);
        return users;
    }

    @Override
    public User findByCondition(User user) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
        User user2 = (User) sqlSession.selectOne("user.selectOne", user);
        System.out.println("user2 = " + user2);
        return user2;
    }
}